<template>
  <div class="match-page">
    <div class="title">AI智能笔匹配中 {{matchingPenData.length}}/{{ coursePenData.length }}</div>

    <n-scrollbar style="height: calc(100% - 162px)">
      <div class="student-wrap">
        <div
          class="student-item"
          v-for="(item, i) in coursePenData"
          :key="i"
          @click="dialogTap({ name: 'swap', detail: item })"
        >
          <div class="item-top">
            <avatar class="avatar" :size="72" :src="item?.studentInfo?.pic" />
            <div class="desc-info">
              <div class="student-name">
                {{ item.studentInfo ? item.studentInfo.name : '等待匹配' }}
                <div class="swap">
                  <n-icon size="18">
                    <SwapOutlined />
                  </n-icon>
                </div>
              </div>
              <div class="student-label" v-if="item.studentInfo"
                >{{ getEnumDataLabel('gender', item.studentInfo.gender) }} |
                {{ item.studentInfo.age }}岁 | {{ item.studentInfo.learningStageName }}</div
              >
            </div>
            <template v-if="item.status === 1">
              <div class="status" v-if="item.studentInfo">
                <n-icon size="30">
                  <CheckmarkCircleOutline color="#28d6b3" />
                </n-icon>
                &nbsp;已匹配
              </div>
              <div class="status" v-else>
                <n-icon size="30">
                  <CheckmarkCircleOutline color="#28d6b3" />
                </n-icon>
                &nbsp;在线
              </div>
            </template>
            <template v-else>
              <div class="status" v-if="item.studentInfo">
                <n-icon size="30">
                  <CheckmarkCircleOutline color="#676b70" />
                </n-icon>
                &nbsp;未匹配
              </div>
              <div class="status" v-else>
                <n-icon size="30">
                  <CheckmarkCircleOutline color="#676b70" />
                </n-icon>
                &nbsp;离线
              </div>
            </template>
          </div>
          <div class="hr"></div>
          <div class="bluetooth-info">
            <!--            <div class="info-item">-->
            <!--              <div class="label">笔名:</div>-->
            <!--              <div class="text">D1V8-826C</div>-->
            <!--            </div>-->
            <div class="info-item">
              <div class="label">MAC:</div>
              <div class="text">{{ item.mac }}</div>
            </div>
          </div>
        </div>
      </div>
    </n-scrollbar>
    <div class="bottom-wrap">
      <n-button type="primary" size="large" @click="nextTap">下一步</n-button>
    </div>
    <swapStudent
      v-if="dialogState.visible"
      :pen-obj="dialogState.detail"
      @close="dialogClose"
      @change="swapStudentChange"
    />
  </div>
</template>

<script setup lang="ts">
  import { computed, onMounted, onUnmounted } from 'vue';
  import { CheckmarkCircleOutline } from '@vicons/ionicons5';
  import { SwapOutlined } from '@vicons/antd';
  import { useDataTable } from '@/hooks/useDataTable';
  import swapStudent from './components/swapStudent.vue';
  import { useCourse } from '@/store/modules/course';
  import { getTeacherStudentList } from '@/api/course/index';
  import { getEnumDataLabel } from '@/enums/dataEnum';

  const { dialogTap, dialogState, dialogClose, router } = useDataTable();
  const studentData = computed(() => {
    return useCourse().getStudentData;
  });
  const coursePenData = computed(() => {
    return useCourse().getCoursePenData;
  });
  const matchingPenData = computed(() => {
    return useCourse().getCoursePenData.filter((item) => item.studentInfo);
  });
  function swapStudentChange(e) {
    dialogClose();
  }
  onMounted(async () => {
    useCourse().connectWebsocket();
    if (studentData.value.length === 0) {
      getTeacherStudentList({ pageNum: 1, pageSize: 100 }).then((res) => {
        useCourse().setStudentData(res.records);
      });
    }
    console.log(Date.now());
  });

  function nextTap() {
    // router.push({
    //   name: 'CourseDuringClass',
    // });
    useCourse().setPageRoute('duringClass')
  }
</script>

<style scoped lang="less">
  .match-page {
    height: 100%;
    display: flex;
    flex-direction: column;

    .title {
      font-size: 48px;
      line-height: 60px;
      margin-bottom: 30px;
    }
    .bottom-wrap {
      position: fixed;
      left: 0;
      right: 0;
      bottom: 0;
      height: 108px;
      background-color: #fff;
    }
  }
  .student-wrap {
    display: flex;
    flex-wrap: wrap;
    //justify-content: space-between;
  }
  .student-item {
    width: 450px;
    height: 228px;
    background-color: #fff;
    border-radius: 12px;
    padding: 24px;
    //cursor: pointer;
    margin-bottom: 18px;
    margin-right: 16px;
    cursor: pointer;
    &:nth-child(4n + 4) {
      margin-right: 0;
    }
    .item-top {
      display: flex;
      align-items: center;
      .avatar {
        width: 72px;
        height: 72px;
        border-radius: 50%;
      }
      .desc-info {
        flex: 1;
        margin: 0 16px;
      }
      .student-name {
        font-size: 24px;
        display: flex;
        align-items: center;
        .swap {
          margin-left: 6px;
          width: 24px;
          height: 24px;
          display: flex;
          align-items: center;
          justify-content: center;
          border: 1px solid #dadee0;
          border-radius: 6px;
          cursor: pointer;
        }
      }
      .student-label {
        font-size: 21px;
        color: #9ba2aa;
      }
      .status {
        height: 72px;
        display: flex;
        font-size: 21px;
        line-height: 30px;
      }
    }
    .hr {
      width: 100%;
      height: 2px;
      background: #e6eaed;
      margin: 18px 0;
    }
    .bluetooth-info {
      font-size: 21px;
      line-height: 32px;
      color: #676b70;
      .info-item {
        display: flex;
        margin-bottom: 4px;
        &:last-child {
          margin-bottom: 0;
        }
        .label {
          margin-right: 18px;
        }
      }
    }
  }
  .bottom-wrap {
    display: flex;
    justify-content: center;
    align-items: center;
    .n-button {
      font-size: 28px;
      width: 300px;
      height: 72px;
      border-radius: 12px;
    }
  }
</style>
